import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.DefaultListModel;


public class FileIO {
	
	private final static Charset ENCODING = StandardCharsets.UTF_8; 

	public static Vector<String> readPathsFromFile(String pathsFilePath) throws IOException {
		Path fFilePath = Paths.get(pathsFilePath);
		Vector<String> list = new Vector<String>();
		if(fFilePath.toFile().exists()){
			try (Scanner scanner = new Scanner(fFilePath, ENCODING.name())){
				while (scanner.hasNextLine()){
					list.addElement(processLine(scanner.nextLine()));
				}
			}			
		}else{
			System.out.println("File not existing!");
		}
		return list;
	}

	
	private static String processLine(String aLine) {
		
		//use a second Scanner to parse the content of each line 
	    Scanner scanner = new Scanner(aLine);
	    //scanner.useDelimiter("=");
	    if (scanner.hasNext()){
	      //assumes the line has a certain structure
	       return scanner.next();
	      //String value = scanner.next();
	      //log("Name is : " + quote(name.trim()) + ", and Value is : " + quote(value.trim()));
	    }
	    else {
	      System.out.println("Empty or invalid line. Unable to process.");
	    }
	    return null;
	}
	
	private String quote(String aText){
	    String QUOTE = "'";
	    return QUOTE + aText + QUOTE;
	  }


	public static void writePaths(String pathsFilePath,
			DefaultListModel<String> listModel) throws IOException {
		Path path = Paths.get(pathsFilePath);
		
		Vector<String> vector = new Vector<String>();
		for (int i = 0; i < listModel.getSize(); i++) {
			vector.add(listModel.get(i));
		}
		
	    Files.write(path, vector, ENCODING);
	}
 
}
